package com.explorer.ider.bootguide.views;

import android.content.Context;
import android.graphics.Canvas;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.ListView;

/**
 * Created by Eric on 2016/9/20.
 */
public class MyScrollListView extends ListView{
    public MyScrollListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyScrollListView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            handler.removeCallbacks(run_scroll_down);
            handler.removeCallbacks(run_scroll_up);
        }
    };

    /**
     * 向上滚动
     */
    public void listScrollUp() {
        listScrollOff();
        handler.postDelayed(run_scroll_up, 0);
    }

    /**
     * 向下滚动
     */
    public void listScrollDown() {
        listScrollOff();
        handler.postDelayed(run_scroll_down, 0);
    }

    /**
     * 停止滚动
     */
    public void listScrollOff() {
        handler.removeCallbacks(run_scroll_down);
        handler.removeCallbacks(run_scroll_up);
    }

    Runnable run_scroll_up = new Runnable() {
        @Override
        public void run() {
            /**
             * public void smoothScrollBy (int distance, int duration)
             *
             * Added in API level 8 Smoothly scroll by distance pixels over duration milliseconds.
             *
             * Parameters
             *     distance Distance to scroll in pixels.
             *     duration Duration of the scroll animation in milliseconds.
             */
            MyScrollListView.this.smoothScrollBy(1, 10);
            handler.postDelayed(run_scroll_up, 10);
        }
    };
    Runnable run_scroll_down = new Runnable() {
        @Override
        public void run() {
            MyScrollListView.this.smoothScrollBy(-1, 10);
            handler.postDelayed(run_scroll_down, 10);
        }
    };

}
